import { SetTaskScore, GetEditVersion } from "@/api";
import { aesEncrypt } from "@/utils/encrypt.js";


export const jumpUrl = (url) => {
    const { origin, search } = GetUrlParams(url);
    const newUrl = `${origin}?${aesEncrypt(search)}`;
    const pageStore = usePageStore();
    window.open(newUrl, pageStore.isMobile ? "_self" : "_blank");
};

export const onMakeModel = async (item) => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const dialogStore = useDialogStore();
    let url = GetTemplateLink2(item.url, item.templatestype_code);
    if (userStore.isLogin) {
        if (pageStore.isMobile) {
            const res = await GetEditVersion();
            if (localStorage.getItem("editor_version") !== res.data) {
                localStorage.setItem("editor_version", res.data);
                dialogStore.isEditorUpdateTipsDialogVisible = true;
            } else {
                Message().warning("请在电脑端网站打开！网址www.ykjhr.com");
            }
        } else {
            if (['shuju', 'yejin', 'zonghe'].includes(item.templatestype_code)) {
                if (userStore.userInfo.level == 3) {
                    jumpUrl(url + "&uid=" + userStore.userInfo.id);
                } else {
                    ElMessageBox.confirm("", "请联系客服咨询", {
                        "show-close": false,
                        closeOnClickModal: false,
                        closeOnPressEscape: false,
                        showCancelButton: false,
                        autofocus: false,
                        lockScroll: false,
                        confirmButtonText: "确认",
                    })
                }
            } else {
                jumpUrl(url + "&uid=" + userStore.userInfo.id);
            }
        }
    } else {
        const { origin, search } = GetUrlParams(url);
        LoginModal(
            true,
            pageStore.isMobile || (['shuju', 'yejin', 'zonghe'].includes(item.templatestype_code))
                ? {}
                : {
                    url: origin,
                    search,
                    params: ["uid"],
                    encrypt: true,
                },
            {},
            !pageStore.isMobile
        );
    }
};

export const onPreview = async (item) => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    let url = GetWorkLink2(item.preview_url, item.templatestype_code);

    if (userStore.isLogin) {
        const uid = userStore.userInfo.id || "0";
        await SetTaskScore({ code: 1002, _id: item.id });
        // item.page_view = item.page_view + 1;
        jumpUrl(url + ("&uid=" + uid));
    } else {
        const { origin, search } = GetUrlParams(url);
        LoginModal(
            true,
            {
                url: origin,
                search,
                params: ["uid"],
                encrypt: true,
            },
            {
                code: 1002,
                _id: item.id,
            },
            !pageStore.isMobile
        );
    }
};

export const onBuyModel = async (item) => {
    if (item.templatestype_code != "template" && item.templatestype_code != "courseware") {
        ElMessageBox.confirm("", "请联系客服咨询", {
            "show-close": false,
            closeOnClickModal: false,
            closeOnPressEscape: false,
            showCancelButton: false,
            autofocus: false,
            lockScroll: false,
            confirmButtonText: "确认",
        })
        return;
    }
    const pageStore = usePageStore();
    navigateTo("buyDetail/" + item.id + ".html", {
        open: !pageStore.isMobile,
    });
};

export const IsNeedPay = ({ price, templatestype_code }) => {
    if (templatestype_code != "template" && templatestype_code != "courseware") {
        return true;
    }
    for (let item of price) {
        if (item.price > 0) return true;
    }
    return false;
};


export const onShare = (item) => {
    const dialogStore = useDialogStore();
    dialogStore.isShareTemplateDialogVisible = true;

    let url = GetTemplateLink2(item.url, item.templatestype_code);
    const { origin, search } = GetUrlParams(url);
    const newUrl = `${origin}?${aesEncrypt(search)}`;

    dialogStore.shareTitle = "模板链接";
    dialogStore.shareUrl = newUrl;
    dialogStore.shareName = item.name;

}